﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using System.Xml.Serialization;
using Tomahawk.Runtime.Canvas.Sprite;
using Tomahawk.Runtime.Rendering.Interfaces;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;

namespace Tomahawk.Runtime.Logic.UI.Components
{
    /// <summary>
    /// Reusable simple Image renderer
    /// </summary>
    public struct ImageComponent 
    {
        /// <summary>
        /// Indicates the texture this image component will use
        /// </summary>
        [XmlAttribute("texture")]
        public string textureAsset;

        private UISprite sprite;
        
        /// <summary>
        /// Initialization
        /// </summary>
        public void Initialize()
        {
            if ((this.textureAsset != null) && (this.textureAsset != ""))
            {
                //Create an UISprite primitive and register it
                sprite = new UISprite(this.textureAsset);
                Engine.Instance.CanvasManager.CPR_UISprites.AddPrimitive(sprite);
            }
        }

        /// <summary>
        /// Changes the visibility of the component
        /// </summary>
        public void SetVisibility(bool value)
        {
            if (sprite != null)
            {
                sprite.Visible = value;
            }
        }

        /// <summary>
        /// Changes the position of the component
        /// </summary>
        public void SetPosition(ref Rectangle rect)
        {
            if (sprite != null)
            {
                sprite.DestinationRectangle.X = rect.X;
                sprite.DestinationRectangle.Y = rect.Y;
                sprite.DestinationRectangle.Width = rect.Width;
                sprite.DestinationRectangle.Height = rect.Height;
            }
        }

        /// <summary>
        /// Returns if the component is valid (i.e. correctly initialized)
        /// </summary>
        public bool IsValid()
        {
            return (sprite != null);
        }

        /// <summary>
        /// Destruction
        /// </summary>
        public void Destroy()
        {
            if (sprite != null)
            {
                Engine.Instance.CanvasManager.CPR_UISprites.RemovePrimitive(sprite);
            }
            sprite = null;
        }

        public bool IsReady()
        {
            bool retVal = true;
            if (sprite != null) retVal = sprite.IsReady;
            return retVal;
        }

    }

}
